
use serde::Deserialize;
use std::fs::File;
use std::io::Read;
use toml;


#[derive(Deserialize, Debug)]
pub struct Config {
    pub server: Server,
    pub db: DB,
}


#[derive(Deserialize, Debug)]
pub struct Server {
    pub addr: String,
}

#[derive(Deserialize, Debug)]
pub struct DB {
    pub host: String,
    pub port: u32,
    pub user: String,
    pub passwd: String,
    pub database: String,
}


impl Config {

    // 读取配置文件生成config结构体
    pub fn read_to_config(config_file_path:String) -> Config {
        let mut file = File::open(config_file_path).expect("Unable to open config file");
        let mut contents = String::new();

        file.read_to_string(&mut contents).expect("Unable to read config file");
        let config: Config = toml::from_str(&contents).expect("Unable to parse config");

        config
    }

    pub fn get_database_url(&self) -> String {
        let database_url = format!("mysql://{}:{}@{}:{}/{}",self.db.user,self.db.passwd,self.db.host,self.db.port,self.db.database);
        database_url
    }

}




